//
//  ViewController.m
//  ZBLib
//
//  Created by  on 11-12-7.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "HorizontalController.h"
#import "VerticalController.h"
#import "ZBWebController.h"

@implementation ViewController

#define Layout @"Layout"
#define VerticalLayout @"VerticalLayout"
#define HorizontalLayout @"HorizontalLayout"
#define Others @"Others"
#define WebController @"WebController"

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"ZBLib";
	// Do any additional setup after loading the view, typically from a nib.
    if (!_tableDic) {
        _tableDic =[[NSMutableDictionary alloc] initWithCapacity:10];
        [_tableDic setObject:[NSArray arrayWithObjects:VerticalLayout,HorizontalLayout, nil]
                      forKey:Layout];
        [_tableDic setObject:[NSArray arrayWithObjects:WebController, nil] forKey:Others];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)dealloc {
    [_tableDic release];
    [super dealloc];
}

#pragma mark - Tablview datasource delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_tableDic count];  
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString * key =[[_tableDic allKeys] objectAtIndex:section];
    return [[_tableDic objectForKey:key] count];
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_tableDic allKeys] objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const identifier =@"CellIdentifier";
    UITableViewCell *cell =[self.tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    NSString * key =[[_tableDic allKeys] objectAtIndex:indexPath.section];
    cell.textLabel.text = [[_tableDic objectForKey:key] objectAtIndex:indexPath.row];
    
    return cell;
 }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * key =[[_tableDic allKeys] objectAtIndex:indexPath.section];
    NSString * command = [[_tableDic objectForKey:key] objectAtIndex:indexPath.row]; 
    UIViewController *controller = nil;
    if (command==VerticalLayout) {
        controller =[[[VerticalController alloc] init] autorelease]; 
    } else if (command==HorizontalLayout) {
        controller =[[[HorizontalController alloc] init] autorelease];        
    } else if (command==WebController) {
        ZBWebController *webController =[[[ZBWebController alloc] init] autorelease];
        webController.url = [NSURL URLWithString:@"http://www.163.com"];
        controller = webController;
    }
    [self.navigationController pushViewController:controller animated:YES];
}

@end
